package com.chenshu.shop.service;

import com.chenshu.shop.mapper.TypeMapper;
import com.chenshu.shop.model.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-06-06
 * Time：09:08
 */
@Service
@Slf4j
public class TypeService {
    @Autowired
    TypeMapper typeMapper;
    public List<Type> getAllTypes() {
        return typeMapper.selectAllTypes();
    }

    @Transactional(rollbackFor = Exception.class)
    public List<Type> deleteType(Integer id) {
        Integer row = typeMapper.deleteType(id);
        if (row == 1) {
            log.info("delete type success");
        } else {
            log.info("delete type fail");
        }
        return getAllTypes();
    }

    @Transactional(rollbackFor = Exception.class)
    public List<Type> saveType(String name, Integer num) {
        Integer row = typeMapper.addType(name, num);
        if (row == 1) {
            log.info("save type success");
        } else {
            log.info("save type fail");
        }
        return getAllTypes();
    }

    @Transactional(rollbackFor = Exception.class)
    public List<Type> updateType(Type type) {
        Integer row = typeMapper.updateType(type);
        if (row == 1) {
            log.info("修改类目成功");
        } else {
            log.info("修改类目失败");
        }
        return getAllTypes();
    }

    public Type getTypeById(Integer id) {
        Type type = typeMapper.selectTypeById(id);
        if (type == null) {
            log.info("通过id未查到类目");
        } else {
            log.info("通过id查询到类目");
        }
        return type;
    }
}
